import {DirectoryTreeNode, FileIndexItem} from "../../filespace/src";
import {ElMessage} from "element-plus";

export const useDirectoryStore = defineStore('directory', () => {
  const folders = ref<DirectoryTreeNode[]>([])

  // 加载文件夹列表
  const loadFolders = async () => {
    window.bnote.dirs.tree().then((res: DirectoryTreeNode) => {
      folders.value = res.children || []
    })
  }

  // 创建文件夹
  const createFolder = async (name: string, parentId?: string) => {
    window.bnote.dirs.create(name, parentId).then((res: FileIndexItem | null) => {
      if (res) {
        loadFolders()
      } else {
        ElMessage.error('创建文件夹失败，请重试')
      }
    })
  }

  // 删除文件夹
  const deleteFolder = async (dirId: string) => {
    window.bnote.dirs.delete(dirId).then((res: boolean) => {
      if (res) {
        loadFolders()
      } else {
        ElMessage.error('删除文件夹失败，请重试')
      }
    })
  }

  // 移动文件夹
  const moveFolder = async (dirId: string, parentId?: string) => {
    window.bnote.dirs.move(dirId, parentId).then((res: boolean) => {
      if (!res) {
        ElMessage.error('移动文件夹失败，请重试')
      }
    })
  }

  return {
    folders,
    loadFolders,
    createFolder,
    deleteFolder,
    moveFolder
  }
})
